package repository.impl;


import cn.hutool.core.util.ObjectUtil;
import database.BeePool;
import repository.CharacterMaterialMapper;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 材料仓库数据库操作
 * @Author: cc
 */
public class CharacterMaterialImpl implements CharacterMaterialMapper {

    @Override
    public void saveOne(int characterId, int itemId, int count, int type) {
        String sql = "INSERT INTO character_material (character_id, item_id ,count, type, create_time) VALUES (?, ?, ?, ?, ?)";
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, characterId);
            ps.setInt(2, itemId);
            ps.setInt(3, count);
            ps.setInt(4, type);
            ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void saveBatch(List<Map<String, Integer>> batch) {
        String sql = "INSERT INTO character_material (character_id, item_id,count,type,create_time) VALUES (?, ?, ?, ?,?)";
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            connection.setAutoCommit(false);
            for (int i = 0; i < batch.size(); i++) {
                ps.setInt(1, batch.get(i).get("character_id"));
                ps.setInt(2, batch.get(i).get("item_id"));
                ps.setInt(3, batch.get(i).get("count"));
                ps.setInt(4, batch.get(i).get("type"));
                ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
                ps.addBatch();
                if (i % 100 == 0) {
                    ps.executeBatch();
                    connection.commit();
                    ps.clearBatch();
                }
            }
            ps.executeBatch();
            connection.commit();
            ps.clearBatch();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void updateCount(int count, int itemId, int characterId) throws Exception {
        //先查询是否存在当前道具
        Map<String, Object> map = queryByUserOne(characterId, itemId);
        if (ObjectUtil.isNotEmpty(map)) {
            String sql = "UPDATE character_material SET count = count + ?,update_time = ? WHERE character_id = ? AND item_id = ?";
            try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
                ps.setInt(1, count);
                ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
                ps.setInt(3, characterId);
                ps.setInt(4, itemId);
                ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return;
        }
        //不存在就新增
        saveOne(characterId, itemId, count, 4);
    }


    @Override
    public void deleteBatch(int characterId) {
        String sql = "DELETE FROM character_material WHERE character_id = ?";
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, characterId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void deleteOne(int characterId, int itemId) {
        String sql = "DELETE FROM character_material WHERE character_id = ? AND item_id = ?";
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, characterId);
            ps.setInt(2, itemId);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public Map<String, Object> queryByUserOne(int characterId, int itemId) throws Exception {
        String sql = "SELECT id, character_id, item_id, count, type FROM character_material WHERE character_id = ? AND item_id = ?";
        Map<String, Object> map = null;
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, characterId);
            ps.setInt(2, itemId);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                map = getRows(resultSet, new HashMap<>(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }


    @Override
    public List<Map<String, Object>> queryByUserList(int characterId) throws Exception {
        String sql = "SELECT id, character_id, item_id, count, type FROM character_material WHERE character_id = ?";
        List<Map<String, Object>> maps = new ArrayList<>();
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, characterId);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                maps.add(getRows(resultSet, new HashMap<>(5)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return maps;
    }


    @Override
    public List<Map<String, Object>> queryList() throws Exception {
        String sql = "SELECT id, character_id, item_id, count, type FROM character_material";
        List<Map<String, Object>> maps = new ArrayList<>();
        try (Connection connection = BeePool.getConnection(); PreparedStatement ps = connection.prepareStatement(sql)) {
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                maps.add(getRows(resultSet, new HashMap<>(5)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return maps;
    }

    private static Map<String, Object> getRows(ResultSet row, Map<String, Object> map) throws Exception {
        map.put("id", row.getInt("id"));
        map.put("character_id", row.getInt("character_id"));
        map.put("item_id", row.getInt("item_id"));
        map.put("count", row.getInt("count"));
        map.put("type", row.getInt("type"));
        return map;
    }
}
